package org.administer.contaroller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.administer.dao.CrmCustomerDao;
import org.administer.dao.CrmOrderDao;
import org.administer.dao.CrmOrderDetailsDao;
import org.administer.dao.CrmShoppingDao;
import org.administer.pojo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private CrmOrderDao crmOrderDao;
    @Autowired
    private CrmCustomerDao crmCustomerDao;
    @Autowired
    private CrmShoppingDao crmShoppingDao;
    @Autowired
    private CrmOrderDetailsDao crmOrderDetailsDao;

    @RequestMapping("/index")
    public String index() throws Exception{
        return "order/orderindex";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit,CrmOrder crmOrder)throws Exception{
        Map<String,Object> map=new HashMap<>();
        Page<CrmOrder> pager= PageHelper.startPage(page,limit);
        List<CrmOrder> list=crmOrderDao.getlist(crmOrder);
        map.put("code",0);
        map.put("msg",null);
        map.put("data",list);
        map.put("count",pager.getTotal());
        return map;
    }

    @RequestMapping("/xiadan")
    public String xiadan() throws Exception{
        return "order/xiadan";
    }

    @RequestMapping("/jiesuan")
    public String jiesuan(Integer[] ids,Integer[] xznum,HttpSession session) throws Exception{

        session.setAttribute("ids",ids);
        session.setAttribute("xznum",xznum);
        Integer [] num= (Integer[]) session.getAttribute("xznum");
        System.out.println(xznum);
        return "order/jiesuan";
    }

    @RequestMapping("/getshopp")
    @ResponseBody
    public Map<String,Object> getshopp(HttpSession session,@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) throws Exception{
        Map<String,Object> map=new HashMap<>();
        Integer [] ids= (Integer[]) session.getAttribute("ids");
        Page<CrmShopping> pager= PageHelper.startPage(page,limit);
        List<CrmShopping> list=crmShoppingDao.getshop(ids);
        map.put("code",0);
        map.put("msg",null);
        map.put("data",list);
        map.put("count",pager.getTotal());
        return map;
    }

    @RequestMapping("/jine")
    public String jine(Map map,String ids,HttpSession session) throws Exception{
        Integer jine=0;
        Integer money=0;
        Integer zong=0;
        List<String> list= Arrays.asList(ids.split(","));
        for (String shu:list){
            CrmShopping crmShopping=crmShoppingDao.get(Integer.parseInt(shu));
            jine+=crmShopping.getFuzhu();
            money+=(crmShopping.getPrice()*crmShopping.getFuzhu());
        }
        map.put("jine1",jine);
        map.put("money1",money);
        session.setAttribute("jine",jine);
        session.setAttribute("money",money);
        System.out.println("总数量"+jine);
        System.out.println("金钱"+money);
        return "order/orderindex";
    }

    @RequestMapping("/updatefor")
    @ResponseBody
    public Map<String,Object> updatefor(String ids,String shuliang) throws Exception{
        Map<String,Object> map=new HashMap<>();
        List<String> list= Arrays.asList(ids.split(","));
        CrmShopping crmShopping=new CrmShopping();
         int i =0;
         List<String> list1=Arrays.asList(shuliang.split(","));
        System.out.println("1"+list);
        System.out.println("2"+list1);
        for (String idd:list){
            i++;
            System.out.println("编号"+idd);
            crmShopping.setFuzhu(Integer.parseInt(list1.get(i-1)));
            System.out.println("数量"+list1.get(i-1));
            crmShopping.setId(Integer.parseInt(idd));
            crmShoppingDao.forupdate(crmShopping);
        }
        map.put("code",0);
        map.put("msg",null);
        map.put("data",list);
        return map;
    }
    @PostMapping(value = "/bathadd")
    @ResponseBody
    public Map<String, Object> bathadd(@Param("model") CrmCustomers model, @Param("ordered") String ordered) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        System.out.println(model.getList());
        System.out.println(ordered);
        crmOrderDetailsDao.bathadd(model.getList(),ordered);
        map.put("result",true);
        return map;
    }
    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> add(CrmOrder crmOrder) throws Exception{
        Map<String,Object> map=new HashMap<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String time=sdf.format(new Date());
//        System.out.println(time);
        int min = 1000000;
        int max = 9999999;
        for(int i=0;i<1;i++){
            int aa= new Random().nextInt(max-min)+min;
            crmOrder.setOrderno(Integer.toString(aa));
        }
        crmOrder.setTIME(time);
        crmOrderDao.add(crmOrder);
        return map;
    }
    @RequestMapping("/chaxun")
    public String chaxun(HttpSession session) throws Exception{
        return "order/orderchaxun";
    }
    @RequestMapping("/getchaxun")
    @ResponseBody
    public Map<String,Object> getchaxun(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit,CrmOrder crmOrder)throws Exception{
        Map<String,Object> map=new HashMap<>();
        Page<CrmOrder> pager= PageHelper.startPage(page,limit);
        List<CrmOrder> list=crmOrderDao.getchaxun(crmOrder);
        map.put("code",0);
        map.put("msg",null);
        map.put("data",list);
        map.put("count",pager.getTotal());
        return map;
    }
    @RequestMapping("/xiangqing")
    @ResponseBody
    public Map<String,Object> xiangqing(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit,String Ordered) throws Exception{
        Map<String,Object> map=new HashMap<>();
        Page<CrmOrderDetails> page1=PageHelper.startPage(page,limit);
        System.out.println(Ordered);
        List<CrmOrderDetails> list=crmOrderDetailsDao.xiangqing(Ordered);
        System.out.println("集合"+crmOrderDetailsDao.xiangqing(Ordered).size());
        if(crmOrderDetailsDao.xiangqing(Ordered).size()==0){
            map.put("code",1);
            map.put("msg","你没有购买的商品，无法收货...");
            return map;
        }
        map.put("code",0);
        map.put("msg",null);
        map.put("data",list);
        map.put("count",page1.getTotal());
        return map;
    }
}
